
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}


const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
    children:[
      {
        path: '/home/popup',
        name: 'popup',
        component: () => import(/* webpackChunkName: "popup" */ '../views/Popup.vue'),
        meta:{
          keepAlive:true
        }
      }
    ]
  },
  {
    path:'/subject',
    name:'subject',
    component:()=>import('../views/Subject.vue'),
    meta:{
      index:1
    }
  },
  {
    path:'/classify',
    name:'classify',
    component:()=>import('../views/Classify.vue'),
    meta:{
      index:1
    },
    children:[
      {
        path: '/classify/popup',
        name: 'cpopup',
        component: () => import( '../views/Popup.vue'),
        meta:{
          keepAlive:true
        }
      }
    ]
  },
  // 购物车
  {
    path:"/cart",
    name:'cart',
    component:()=>import('../views/Cart.vue')
  },
  // 我的
  {
    path:"/my",
    name:'my',
    component:()=>import('../views/My.vue')
  },
  // 频道
  {
    path:"/channel",
    name:'channel',
    component:()=>import('../views/Channel'),
    meta:{
      keepAlive:true
    }
  },
  // 品牌
  {
    path:'/brand',
    name:'brand',
    component:()=>import('../views/Brand'),
    meta:{
      keepAlive:true
    }
  },
  // 产品明细
  {
    path:'/details',
    name:'details',
    component:()=>import('../views/GoodsDetail'),
    meta:{
      keepAlive:true
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  if(localStorage.getItem("token")){
    next()
  }else{
     if(to.path=="/cart"){
        next("/my")
        if(localStorage.getItem("token")){
           next()
        }else{
          return
        }
     }
  }
  next()
})

// router.beforeEach((to,from,next)=>{
//   // to去的路由
//   // from 从哪里来
//   const token = localStorage.getItem('token');
  
//     if (token) {
//       // if (to.path == '/cart') {
//       //   next();
//       // }else{
//       //   next("");
//       // }
//       next();
//     }else{
//       // sessionStorage.setItem('iconIndex',JSON.stringify(4));
//       // this.$store.state.active = 4
//       if (to.path == '/cart') {
//         // this.$store.commit('Tabbar_change', 4);
//         next('/my');
//         // location.reload('/my');
//         sessionStorage.setItem('iconIndex',4)
//       }else{
//         next();
//       }
//     }
// })


export default router
